package org.jeecg.modules.solitaire_app.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.solitaire_app.app.entity.YsyUserTicket;
import org.jeecg.modules.solitaire_app.app.service.IYsyUserTicketService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 用户罚单通知
 * @Author: jeecg-boot
 * @Date: 2023-09-09
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "用户罚单通知")
@RestController
@RequestMapping("/app/ysyUserTicket")
public class YsyUserTicketController extends JeecgController<YsyUserTicket, IYsyUserTicketService> {
    @Autowired
    private IYsyUserTicketService ysyUserTicketService;

    /**
     * 分页列表查询
     *
     * @param ysyUserTicket
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "用户罚单通知-分页列表查询")
    @ApiOperation(value = "用户罚单通知-分页列表查询", notes = "用户罚单通知-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyUserTicket ysyUserTicket,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsyUserTicket> queryWrapper = QueryGenerator.initQueryWrapper(ysyUserTicket, req.getParameterMap());
        Page<YsyUserTicket> page = new Page<YsyUserTicket>(pageNo, pageSize);
        IPage<YsyUserTicket> pageList = ysyUserTicketService.page(page, queryWrapper);

        UpdateWrapper<YsyUserTicket> updateWrapper = Wrappers.update();
        updateWrapper.eq("user_id", ysyUserTicket.getUserId());
        YsyUserTicket ysyUserTicket1 = new YsyUserTicket();
        ysyUserTicket1.setIsRead(1);
        ysyUserTicketService.update(ysyUserTicket1, updateWrapper);

        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "用户罚单通知-获取未读条数")
    @ApiOperation(value = "用户罚单通知-获取未读条数", notes = "用户罚单通知-获取未读条数")
    @GetMapping(value = "/getNoReadNum/{userId}")
    public Result<?> getNoReadNum(@PathVariable("userId") String userId) {
        int count = ysyUserTicketService.count(Wrappers.<YsyUserTicket>lambdaQuery()
                .eq(YsyUserTicket::getUserId,userId)
                .eq(YsyUserTicket::getIsRead,0));
        return Result.ok(count);
    }



    /**
     * 添加
     *
     * @param ysyUserTicket
     * @return
     */
    @AutoLog(value = "用户罚单通知-添加")
    @ApiOperation(value = "用户罚单通知-添加", notes = "用户罚单通知-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyUserTicket ysyUserTicket) {
        ysyUserTicketService.save(ysyUserTicket);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ysyUserTicket
     * @return
     */
    @AutoLog(value = "用户罚单通知-编辑")
    @ApiOperation(value = "用户罚单通知-编辑", notes = "用户罚单通知-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyUserTicket ysyUserTicket) {
        ysyUserTicketService.updateById(ysyUserTicket);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户罚单通知-通过id删除")
    @ApiOperation(value = "用户罚单通知-通过id删除", notes = "用户罚单通知-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ysyUserTicketService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户罚单通知-批量删除")
    @ApiOperation(value = "用户罚单通知-批量删除", notes = "用户罚单通知-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyUserTicketService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户罚单通知-通过id查询")
    @ApiOperation(value = "用户罚单通知-通过id查询", notes = "用户罚单通知-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyUserTicket ysyUserTicket = ysyUserTicketService.getById(id);
        return Result.ok(ysyUserTicket);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyUserTicket
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyUserTicket ysyUserTicket) {
        return super.exportXls(request, ysyUserTicket, YsyUserTicket.class, "用户罚单通知");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyUserTicket.class);
    }

}
